<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Model;

class ProjectMeeting extends Model
{
    use HasDateTimeFormatter;
    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'update_time';
    public $timestamps = true;

    protected $table = 'eb_project_meeting';

    // 会议关联会议讲者
    public function invitationSpeaker()
    {
        return $this->hasMany(ProjectMeetingInvitationSpeaker::class, 'project_meeting_id', 'id');
    }

    /**
     * 只查没有被删除的作用域
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeDeleteTime($query)
    {
        return $query->whereNull('delete_time');
    }


    /**
     * 获取项目下的所有会议
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-11-22
     * @param [type] $payload
     * @return void
     */
    public function getProjectMeetingById($payload)
    {
        $where = function ($query) use ($payload) {
            $project_id = $payload['project_id'];
            $query->where('project_id', $project_id);
            // $query->where('user_id', $userId);
            $query->where('is_display', 1);

            if (isset($payload['meeting_name']) && $payload['meeting_name'] != '') {
                $query->where('meeting_name', 'like', '%' . $payload['meeting_name'] . '%');
            }

            if (isset($payload['meeting_status']) && $payload['meeting_status'] != '') {
                $query->where('status', $payload['meeting_status']);
            }

            if (isset($payload['create_time']) && $payload['create_time'] != '') {
                $query->whereDate('create_time',  $payload['create_time']);
            }
        };

        $list = $this->deleteTime()->where($where)
            ->select(
                'id',
                'project_id',
                'meeting_name',
                'confirm_invitation',
                'total_invitation',
                'status',
                // 'user_id',
                'create_time',
                'stop_remarks',
                'stop_time'
            )
            ->orderBy('create_time', 'desc')
            ->paginate($payload['limit']);

        return $list;
    }
}
